package banking.gui;

import java.awt.BorderLayout;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;

import banking.BankAccountActionController;

/**
 * A basic JFC based application.
 */
public class BankFrm extends javax.swing.JFrame {

	private static final long serialVersionUID = 1L;

	BankAccountActionController controller;

	private DefaultTableModel model;

	private JTable JTable1;

	private JScrollPane JScrollPane1;

	public BankFrm() {
		setTitle("Bank Application.");
		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		getContentPane().setLayout(new BorderLayout(0, 0));
		setSize(600, 320);
		setVisible(false);

		JPanel1.setLayout(null);
		JPanel1.setBounds(0, 0, 575, 350);
		getContentPane().add(BorderLayout.CENTER, JPanel1);

		model = new DefaultTableModel();
		model.addColumn("AccountNr");
		model.addColumn("Name");
		model.addColumn("City");
		model.addColumn("P/C");
		model.addColumn("Ch/S");
		model.addColumn("Amount");

		JTable1 = new JTable(model);
		JTable1.setBounds(0, 0, 420, 0);

		JScrollPane1 = new JScrollPane();
		JScrollPane1.setBounds(12, 72, 444, 180);
		JScrollPane1.getViewport().add(JTable1);
		JPanel1.add(JScrollPane1);

		JButton_PerAC.setText("Add personal account");
		JButton_PerAC.setBounds(24, 20, 192, 33);
		JPanel1.add(JButton_PerAC);

		JButton_CompAC.setText("Add company account");
		JButton_CompAC.setBounds(240, 20, 192, 33);
		JPanel1.add(JButton_CompAC);

		JButton_Addinterest.setText("Add interest");
		JButton_Addinterest.setBounds(468, 20, 106, 33);
		JPanel1.add(JButton_Addinterest);

		JButton_Deposit.setText("Deposit");
		JButton_Deposit.setBounds(468, 70, 106, 33);
		JPanel1.add(JButton_Deposit);

		JButton_Withdraw.setText("Withdraw");
		JButton_Withdraw.setBounds(468, 120, 106, 33);
		JPanel1.add(JButton_Withdraw);

		JButton_GenerateReport.setText("Report");
		JButton_GenerateReport.setBounds(468, 170, 106, 33);
		JPanel1.add(JButton_GenerateReport);

		JButton_Exit.setText("Exit");
		JButton_Exit.setBounds(468, 220, 106, 33);
		JPanel1.add(JButton_Exit);

		controller = BankAccountActionController.getInstance();
		controller.setView(this);
		JButton_PerAC.addActionListener(controller.getCreatePActionListener());
		JButton_CompAC.addActionListener(controller.getCreateCActionListener());
		JButton_Deposit.addActionListener(controller.getDepositActionListener());
		JButton_Withdraw.addActionListener(controller.getWithdrawActionListener());
		JButton_Addinterest.addActionListener(controller.getAddInterestActionListener());
		JButton_GenerateReport.addActionListener(controller.getGenerateReporttActionListener());
		JButton_Exit.addActionListener(controller.getExitActionListener());
	}

//	/*****************************************************
//	 * The entry point for this application. Sets the Look and Feel to the
//	 * System Look and Feel. Creates a new JFrame1 and makes it visible.
//	 *****************************************************/
//	static public void main(String args[]) {
//		try {
//			// Add the following code if you want the Look and Feel
//			// to be set to the Look and Feel of the native system.
//
//			try {
//				UIManager.setLookAndFeel(UIManager
//						.getSystemLookAndFeelClassName());
//			} catch (Exception e) {
//			}
//
//			// Create a new instance of our application's frame, and make it
//			// visible.
//			(new BankFrm()).setVisible(true);
//		} catch (Throwable t) {
//			t.printStackTrace();
//			// Ensure the application exits with an error condition.
//			System.exit(1);
//		}
//	}

	javax.swing.JPanel JPanel1 = new javax.swing.JPanel();
	javax.swing.JButton JButton_PerAC = new javax.swing.JButton();
	javax.swing.JButton JButton_CompAC = new javax.swing.JButton();
	javax.swing.JButton JButton_Deposit = new javax.swing.JButton();
	javax.swing.JButton JButton_Withdraw = new javax.swing.JButton();
	javax.swing.JButton JButton_Addinterest = new javax.swing.JButton();
	javax.swing.JButton JButton_GenerateReport = new javax.swing.JButton();
	javax.swing.JButton JButton_Exit = new javax.swing.JButton();

	void exitApplication() {
		try {
			this.setVisible(false); // hide the Frame
			this.dispose(); // free the system resources
			System.exit(0); // close the application
		} catch (Exception e) {
		}
	}

	public void updateModel(Object[] data) {
		model.addRow(data);
		JTable1.getSelectionModel().setAnchorSelectionIndex(-1);
	}
	
	public void updateModel(int selection, double amount){
		model.setValueAt(String.valueOf(amount), selection, 5);
	} 

	public JTable getJTable1() {
		return JTable1;
	}

	public DefaultTableModel getModel() {
		return model;
	}
}
